package com.server;

import java.util.Iterator;

import org.apache.log4j.Logger;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

import com.gateway.common.Common;
import com.gateway.core.MyBuffer;
import com.gateway.core.MyProtocol;

/**
 * 主要用来应对客户端请求
 * 
 * @author Simple
 * @date 2013-7-5 下午03:41:08
 * @Description TODO
 */
public class MyLogicServerHandler extends IoHandlerAdapter {

  private MyLogicServer logicServer;

  public MyLogicServerHandler(MyLogicServer logicServer) {
    this.logicServer=logicServer;
  }

  private Logger log=Logger.getLogger(this.getClass());

  public void exceptionCaught(IoSession session, Throwable arg1) throws Exception {
    // TODO Auto-generated method stub
  }

  public void messageReceived(IoSession session, Object obj) throws Exception {
    log.debug("[LogicServer receive message]");
    MyProtocol myProtocol=(MyProtocol)obj;
    byte type=myProtocol.getType();
    switch(type) {
      case Common.REQ_EACH:
        log.debug("REQ_EACH");
        MyBuffer buf=new MyBuffer(myProtocol.getBodyData());
        String s=buf.getString();
        log.debug(s);
        break;
    }
  }

  public void messageSent(IoSession session, Object arg1) throws Exception {
    log.debug("[LogicServer send message]");
  }

  public void sessionClosed(IoSession session) throws Exception {
    log.debug("[LogicServer closed session]");
    MyBuffer buffer=MyBuffer.allocate(100);
    buffer.putString(logicServer.getLogicServerName());
    buffer.putString("-");
    buffer.flip();
    MyProtocol myProtocol=new MyProtocol(Common.REQ_NOTICE, buffer.getIoBuffer());
    noticeClient(myProtocol);
  }

  public void sessionCreated(IoSession session) throws Exception {
    log.debug("[LogicServer create session]:" + session);
  }

  public void sessionIdle(IoSession session, IdleStatus arg1) throws Exception {
    log.debug("[LogicServer idle]");
  }

  public void sessionOpened(IoSession session) throws Exception {
    log.debug("[LogicServer opened session]");
    MyBuffer buffer=MyBuffer.allocate(100);
    buffer.putString(logicServer.getLogicServerName());
    buffer.putString("+");
    buffer.flip();
    MyProtocol myProtocol=new MyProtocol(Common.REQ_NOTICE, buffer.getIoBuffer());
    noticeClient(myProtocol);
  }

  /**
   * 通知LogicClient
   * @param myProtocol
   */
  private void noticeClient(MyProtocol myProtocol) {
    NioSocketConnector con=logicServer.getConnector();
    log.debug("LogicClient session count=" + con.getManagedSessionCount());
    Iterator<IoSession> it=con.getManagedSessions().values().iterator();
    while(it.hasNext()) {
      IoSession toGatewaySession=it.next();
      log.debug(toGatewaySession);
      toGatewaySession.write(myProtocol);
      log.debug("LogicServer notice LogicClient");
    }
  }

  public void setLogicServer(MyLogicServer logicServer) {
    this.logicServer=logicServer;
  }

  public MyLogicServer getLogicServer() {
    return logicServer;
  }
}
